<?php
App::uses('AppController', 'Controller');
/**
 * AnsFqas Controller
 *
 * @property AnsFqa $AnsFqa
 * @property PaginatorComponent $Paginator
 */
class AnsFqasController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator','Session');
        public $helpers = array('Form', 'Html', 'Js', 'Time','Session');

/**
 * index method
 *
 * @return void
 */
         public function beforeFilter() {
      
        $this->Auth->allow('display', 'index','view');
         if ($this->Auth->user('role') && $this->Auth->user('role')=='Admin'){ 
              $this->layout='default';
         } else {
        $this->layout='home';
         }
         Controller::loadModel('Menu');
         $this->set('menus', $this->Menu->Find('all', array('conditions'=>array('Menu.position'=>'0'), 'order' => array('Menu.order' => 'asc'))));
         $this->set('menusbottom', $this->Menu->Find('all', array('conditions'=>array('Menu.position'=>'1'), 'order' => array('Menu.order' => 'asc'))));
          $this->set('menusleft', $this->Menu->Find('all', array('conditions'=>array('Menu.position'=>'2'), 'order' => array('Menu.order' => 'asc'))));
         Controller::loadModel('Category');
         Controller::loadModel('Location');
        $nodelist = $this->Category->generateTreeList(null,null,null," - ");

//			$this->set('parents',$this->Category->generatetreelist());
        $this->set('parents',($nodelist));
        $categories = $this->Category->find('list');
        $locations = $this->Location->find('list');
        $this->set(compact('locations'));
        Controller::loadModel('Product');
        if($this->Session->check('order')){
           $data = $this->Session->read('order');
            $favoriteproduct = $this->Product->find('all', array('conditions' => array('Product.id' => $data)));
            $this->set('favoriteproducts', $favoriteproduct);
        }
    }
	public function index() {
		$this->AnsFqa->recursive = 0;
		$this->set('ansFqas', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->AnsFqa->exists($id)) {
			throw new NotFoundException(__('Invalid ans fqa'));
		}
		$options = array('conditions' => array('AnsFqa.' . $this->AnsFqa->primaryKey => $id));
		$this->set('ansFqa', $this->AnsFqa->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add($id=null) {
            if ($this->Auth->user('role') && $this->Auth->user('role')=='Admin'){
		if ($this->request->is('post')) {
			$this->AnsFqa->create();
			if ($this->AnsFqa->save($this->request->data)) {
				$this->Session->setFlash(__('The ans fqa has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ans fqa could not be saved. Please, try again.'));
			}
		}
                $this->set('fqa_id', $id);
		$fqas = $this->AnsFqa->Fqa->find('list');
		$this->set(compact('fqas'));
            }
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
              if ($this->Auth->user('role') && $this->Auth->user('role')=='Admin'){
		if (!$this->AnsFqa->exists($id)) {
			throw new NotFoundException(__('Invalid ans fqa'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->AnsFqa->save($this->request->data)) {
				$this->Session->setFlash(__('The ans fqa has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ans fqa could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('AnsFqa.' . $this->AnsFqa->primaryKey => $id));
			$this->request->data = $this->AnsFqa->find('first', $options);
		}
		$fqas = $this->AnsFqa->Fqa->find('list');
		$this->set(compact('fqas'));
	}
        }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
              if ($this->Auth->user('role') && $this->Auth->user('role')=='Admin'){
		$this->AnsFqa->id = $id;
		if (!$this->AnsFqa->exists()) {
			throw new NotFoundException(__('Invalid ans fqa'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->AnsFqa->delete()) {
			$this->Session->setFlash(__('The ans fqa has been deleted.'));
		} else {
			$this->Session->setFlash(__('The ans fqa could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
              }
	}}
